/*============================================================================*/
/*                     		PANTERS_CUCEI	   		                          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:        GetCamValue.c
* version:         1.0 
* created_by:      Panters  team 
* date_created:    Oct 1 2013 
*=============================================================================*/
/* DESCRIPTION : C source for GetCamValue function                             */
/*============================================================================*/
/* FUNCTION COMMENT : This file describes the C source template according to  */
/* the new software platform                                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 1/10/2013  |                               | PANTERS_CUCEI    */
/* First version of this source                                               */
/*============================================================================*/

/* Includes */
/* -------- */
#include "SCR\GetCamValue.h"
#include "SCR\LineScanCamera.h"
#include "SCR\UART.h"
/* -------- */

/* Exported declarations-- */
/* ----------------------------- */
extern volatile unsigned char AnalogCamera[128];
extern volatile unsigned char DigitalCamera[128];
extern volatile unsigned char center;
/* ----------------------------- */


/**************************************************************
 *  Name                 :	GetCamValue
 *  Description          :	digital filter for camera values.
 *  Parameters           :  
 *  Return               :
 *  Critical/explanation :  send char by char by UART
 **************************************************************/
void GetCamValue(void)
{
	unsigned int r,a,threshold;
	camera();
	
	
	//check the first pixels where we can take set threshold
	threshold=0;
	for (a=THR_TEST_START; a<THR_TEST_END; a++)
	{
		threshold +=AnalogCamera[a];
	}
	threshold = threshold >> 2; //divide by 4
	threshold -= THR_MARG;
	
	//for cycle to get an inverted square signal depending of the umbral of analog signal
	for (r=PIX_START; r<PIX_END; r++)
	{
		if(AnalogCamera[r]<threshold)
		{
			DigitalCamera[r]=1;
			
		}
		else
		{
			DigitalCamera[r]=0;
		}
	//uart0_putchar (UART0_BASE_PTR, DigitalCamera[r]);
	}
	threshold=0;
		
	//AO:			CamData=[255..220 - 90..0 - 255... 220]
	//goal return:	CamData=[00000000000111111100000000000]

	Line_center();
}



/*Line center Deteccion*/
//alexis -----------------
volatile unsigned int xxx = 0;
//-----------------------------
void Line_center(void)
{
	volatile unsigned char i,inicio=0,fin=0;			//i=position vector inicio=start of the line fin=finish of the line
		
		inicio=0;
		fin=0;
	
		for(i=PIX_START;i<PIX_END;i++)			//Checar en que posicion se detecto el flanco de subida
		{
			if(DigitalCamera[i]==1)
			{
				inicio=i;
				break;
			}
		}
		
		
		for(i=inicio+1;i<PIX_END;i++)		//chacar en que posicion se detecto el flanco de bajada
		{
			if(DigitalCamera[i]==0)
			{
				fin=i-1;
				break;
			}
	
		}
		
	
		if((fin==0)&&(inicio!=0))		
		{
			fin=PIX_END;
		}
				
		
		if((inicio+fin==0)||(fin-inicio>=(70))||(fin-inicio<=10))
		{
			center=center;
		}

		else
		{
			/*Center*/
			center=128-((inicio+fin)*0.5);
	
		}
		

}
